package com.lishe.lishe.lishe.module.excitation.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.module.excitation.bean.AchievementObject;
import com.lishe.lishe.lishe.module.excitation.interfaces.OnAchievementCompleteListener;
import com.lishe.lishe.lishe.utils.AnimatorUtils;
import com.nineoldandroids.animation.ObjectAnimator;

/**
 * Created by DELL on 2017/10/30.
 */

public class AchievementCompleteDialog extends Dialog {


    public AchievementCompleteDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    public static class Builder implements View.OnClickListener {
        private Activity context;
        private AchievementObject achievement;
        private AchievementCompleteDialog dialog;
        private OnAchievementCompleteListener listener;
        private ObjectAnimator animator;

        public Builder(Activity context) {
            this.context = context;
        }

        public Builder setAchievementObject(AchievementObject achievement) {
            this.achievement = achievement;
            return this;
        }

        public Builder setOnAchievementCompleteListener(OnAchievementCompleteListener listener) {
            this.listener = listener;
            return this;
        }

        public AchievementCompleteDialog create() throws NullPointerException {
            if (dialog == null) {
                dialog = new AchievementCompleteDialog(context, R.style.MyDialog);
                View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_ahievement_complete, null);
                ImageView img = contentView.findViewById(R.id.img);
                ImageView dissmiss = contentView.findViewById(R.id.dissmiss);
                TextView text = contentView.findViewById(R.id.text);
                TextView reward_size = contentView.findViewById(R.id.reward_size);
                Button get = contentView.findViewById(R.id.get);
                Button show_off = contentView.findViewById(R.id.show_off);

                final ImageView light = contentView.findViewById(R.id.light);
                animator = AnimatorUtils.AnimatorRotation(light);
                animator.start();

                if (achievement == null) {
                    throw new NullPointerException("do you setAchievementObject?");
                } else {
                    ImageLoaderUtils.displayCircleImage(context, achievement.getAchieveImg(), img, R.mipmap.head);
                    text.setText("恭喜达成成就[" + achievement.getAchieveName() + "]");
                    reward_size.setText("奖励积分：" + achievement.getAchieveReward().replace(".00", "") + "积分");
                }
                dissmiss.setOnClickListener(this);
                get.setOnClickListener(this);
                show_off.setOnClickListener(this);
                contentView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        dialog.cancel();
                    }
                });

                dialog.setContentView(contentView);
                dialog.getWindow().setGravity(Gravity.CENTER);
                dialog.setCanceledOnTouchOutside(true);
                dialog.setCancelable(true);
                WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.MATCH_PARENT;
                dialog.getWindow().setAttributes(lp);
            }
            return dialog;
        }

        public void show() {
            try {
                create();
                if (dialog != null && !dialog.isShowing()) {
                    dialog.show();
                }
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }

        public void dissmiss() {
            if (animator != null && animator.isStarted()) {
                animator.cancel();
                animator = null;
            }
            dialog.dismiss();
            dialog.cancel();
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.dissmiss:
                    dialog.dismiss();
                    break;
                case R.id.get:
                    listener.getRewards(dialog, Integer.parseInt(achievement.getAchieveId()));
                    break;
                case R.id.show_off:
                    listener.showOff(dialog, Integer.parseInt(achievement.getAchieveId()));
                    break;
            }
        }
    }

}
